import axios from "axios";  
import {store} from 'redux/index';

let isDev = process.env.NODE_ENV === 'development';
let baseURL;
 

if (isDev) {
    baseURL = "http://huruqing.cn:3006";
} else {
    baseURL = "http://huruqing.cn:3006";
}
const service = axios.create({
  baseURL,
  timeout: 10 * 60 * 1000,
});

//4. 请求拦截
service.interceptors.request.use(
  (config) => {
    let state = store.getState(); 
    config.headers['user-token'] = state.token; 
    return config;
  },
  (err) => {
    // return Promise.reject(err);  
    console.error(err);
  }
);

service.interceptors.response.use(
  (res) => {
    const data = res.data;
    if (data.code != 666) {
      // return Promise.reject(data.msg); 
      console.error(data.msg);
    }
    return data;
  },
  (err) => {
    // return Promise.reject(err);
    console.error('网路异常,请稍后再试');
  }
);

const get = (url, data = {}) => {
  return service.get(url, { params: data });
};
const post = (url, data = {}) => {
  return service.post(url, data);
};

export default {
  get,
  post,
};